var optLabelId = null, subTable = null, labels = [] ;
// 配置
var id = {
    table: 'sys_label_sub_table',
};

var sys_label = {
    form: [
        {"label":"画像编码", "name":"code", "type":"text"},
        {"label":"接收类型", "name":"type", "type":"select", "optionsData":{
                "":"请选择接收类型",
                "text":"一般输入框", "radio":"单选", "multiple":"多选",
                "date":"日期选择", "number":"数字录入框", "textarea":"文本域",
                "edit":"富文本", "image":"单图上传", "images":"多图上传",
                "file": "文件上传", "password":"密码录入框"
            }},
        {"label":"字典编码", "name":"dictCode", "type":"text"},
        {"label":"状态", "name":"state", "type":"select", "optionsData":{
                "1":"启 用", "0":"启 用",

            }, "value":"1"},
        {"label":"排序", "name":"orderNumber", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"},
    ],
    table: {
        id: id.table,
        url: api.base.label + 'ByParent',
        cols:[
            {field: 'name', title: '名称'},
            {field: 'code', title: '编码'},
            {field: 'type', title: '类型'},
            {field: 'dictCode', title: '字典编码'},
            {field: 'orderNumber', title: '排序'},
        ]
    }
}
initialize({
    module:['dtree'],
    title: '数据画像标签管理',
    init: function () {
        subTable = soda.table.render(sys_label.table);
        sys_label_view.tree();
    }
})

var sys_label_view = {
    tree: function(){
        sys_label_data.label.get(function(d){
            labels = d.list;
            dtree.render({
                elem: '#label-tree',
                data: d.tree,
                toolbar:true,
                record:true,
                initLevel: 3,
                toolbarScroll:"#label-tree-toolbar",
                dot:false,
                skin: "layui",  // layui主题风格
                toolbarStyle: {
                    title: "画像",
                    area: ["500px", "600px"]
                },
                toolbarBtn:[
                    sys_label.form,
                    sys_label.form
                ],
                toolbarFun: {
                    addTreeNode: function(treeNode, $div){
                        sys_label_data.label.create(treeNode, function(){
                            sys_label_view.tree();
                            sys_label_data.sub();
                            render.init();
                        });
                    },
                    editTreeLoad: function(treeNode){
                        let node = soda.json.get(labels, treeNode.nodeId);
                        console.log(node)
                        $('select[name="state"]').val(node.state);
                        $('select[name="type"]').val(node.type);
                        $('input[name="orderNumber"]').val(node.orderNumber);
                        $('input[name="code"]').val(node.code);
                        $('input[name="dictCode"]').val(node.dictCode);
                        $('input[name="shortName"]').val(node.shortName);
                        $('textarea[name="remark"]').val(node.remark);
                    },
                    editTreeNode: function(treeNode, $div){
                        sys_label_data.label.update(treeNode, function(){
                            sys_label_view.tree();
                            sys_label_data.sub();
                            render.init();
                        });
                    },
                    delTreeNode: function(treeNode, $div){
                        sys_label_data.label.delete(treeNode, function(){
                            sys_label_view.tree();
                            sys_label_data.sub();
                            render.init();
                        });
                    }
                }
            });


            $(document).on('blur', 'input[name="addNodeName"]', function () {
                let val = $(this).val();
                $('input[name="shortName"]').val(val);
            })
            render.init();

        });
    }
}

var sys_label_data = {
    label: {
        get: function (_callback) {
            let param = {
                url: api.base.label,
                tips: 0,
                callback: _callback
            }
            return soda.http.get(param);
        },
        create: function (_treeNode, _callback) {
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber);
            _treeNode.name = _treeNode.context;
            let param = {
                url: api.base.label,
                data: _treeNode,
                callback: _callback
            }
            return soda.http.post(param);
        },
        update: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber);
            _treeNode.name = _treeNode.context;
            let param = {
                url: api.base.label + '/' + id,
                data: _treeNode,
                callback: _callback
            }
            return soda.http.put(param);
        },
        delete: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            let param = {
                url: api.base.label + '/' + id,
                callback: _callback
            }
            return soda.http.delete(param);
        }
    },
    sub: function () {
        subTable.reload({
            where: {
                page: 1,
                parentId: optLabelId
            }
        })
    }
}

var render = {
    init: function () {
        $('.dtree-layui-item').each(function () {
            let obj = $(this).attr('data-id');
            if(obj == optLabelId){
                $(this).addClass('dtree-layui-item-this')
            }
        })
        dtree.on("node('label-tree')" ,function(obj){
            optLabelId = obj.param.nodeId;
            sys_label_data.sub();
        });

    }
}

